// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.model.validators;

import net.taviscaron.drivenotes.shared.exceptions.InvalidEntityException;
import net.taviscaron.drivenotes.shared.model.entity.Place;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static net.taviscaron.drivenotes.server.util.ExceptionUtils.invalidEntityException;

/**
 * Place validator
 * @author Andrei Senchuk
 */
public class PlaceValidator implements Validator<Place> {
    private static final int NAME_MAX_SIZE = 255;
    private static final Log logger = LogFactory.getLog(NoteValidator.class);

    public enum Error {
        NAME_IS_EMTPY, NAME_IS_TOO_LONG,
        INVALID_LAT, INVALID_LON,
    }

    public void validate(Place place) throws InvalidEntityException {
        if (place.getAuthor() == null) {
            logger.warn("Trying to save place with null author");
            throw invalidEntityException(null);
        }

        if (place.getName() == null || place.getName().trim().isEmpty()) {
            throw invalidEntityException(Error.NAME_IS_EMTPY);
        }

        if (place.getLat() == null || (place.getLat() > 180 || place.getLat() < -180)) {
            throw invalidEntityException(Error.INVALID_LAT);
        }

        if (place.getLon() == null || (place.getLon() > 90 || place.getLon() < -90)) {
            throw invalidEntityException(Error.INVALID_LON);
        }

        if(place.getName().length() > NAME_MAX_SIZE) {
            throw invalidEntityException(Error.NAME_IS_TOO_LONG);
        }
    }
}
